<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\UserForbiddenOp;
use App\Admin\Extensions\UserFreezeOp;
use App\Admin\Extensions\UserOp;
use App\Http\Controllers\Controller;
use App\Model\Message;
use App\Model\SuperAdmin;
use App\Model\User;
use App\Model\UserForbidden;
use App\Model\UserFreezeLog;
use App\Model\UserProfile;
use App\Model\VideoLink;
use App\Util\InfoUtil;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Form\Tools;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class UserForbiddenController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('禁言记录');
            $content->description('');
            $content->body($this->grid());
        });
    }

    public function edit($id)
    {
        $content = new Content();
        $content->header('编辑');
        $content->body($this->form()->edit($id));
        return $content;
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('添加');
            $content->description('');
            $content->body($this->form());
        });
    }

    protected function grid()
    {
        return Admin::grid(UserForbidden::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc', 'adminUser');
            $grid->with(['userProfile', 'user']);
            $grid->start_ts('开始封禁时间')->display(function ($startTs) {
                return date('Y-m-d H:i:s', $startTs);
            });
            $grid->end_ts('结束封禁时间')->display(function ($endTs) {
                return date('Y-m-d H:i:s', $endTs);
            });
            $grid->user_id('内部用户ID');
            $grid->userProfile()->user_profile_id('用户ID');
            $grid->userProfile()->nick_name('用户昵称')->display(function ($nickname) {
                return $nickname;
            });
            $grid->userProfile()->gender('性别')->display(function ($data) {
                return $data == 1 ? '男' : '女';
            });
            $grid->userProfile()->role('用户角色')->display(function ($role) {
                return $role == UserProfile::ROLE_HOST ? '认证女用户' : '普通用户';
            });
            $grid->user()->status('账号状态')->display(function ($data) {
                return User::STATUS[$data] ?? '';
            });
            $grid->reason_type('封禁类型')->display(function ($resonType) {
                return UserFreezeLog::REASON_TYPE[$resonType] ?? '';
            });
            $grid->reason('原因');
            $grid->forbid_mins('账号状态')->display(function ($forbidMins) {
                $day = $forbidMins / 1440;

                return $day >= 3650 ? "永久禁言" : "禁言{$day}天";
            });
            $grid->adminUser()->username('操作人');

            //禁用导出
            $grid->disableExport();

            $grid->actions(function ($actions) {
                $actions->disableDelete();
                $actions->disableView();
                $actions->disableEdit();
                $actions->append(new UserForbiddenOp(
                    $actions->row->user_id,
                    $actions->row,
                    'user/verify'
                ));
            });

            $grid->filter(function ($filter) {
                $filter->expand();
                $filter->disableIdFilter();
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('userProfile.user_profile_id', '用户ID');

                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('user.status', '状态')->select([User::STATUS_OK => '正常', User::STATUS_FREEZE => '冻结', User::STATUS_CANCELLATTED => '已注销', User::STATUS_FORBID => '禁言']);
                });
            });
        });
    }

    protected function form($mode = '', $id = '')
    {
        return Admin::form(UserForbidden::class, function (Form $form) use ($mode, $id) {
            $form->text('user_id', '用户ID');
            $form->select('forbid_mins', '禁言类型')->options([
                1440 => "禁言1天",
                4320 => "禁言3天",
                10080 => "禁言7天",
                20160 => "禁言14天",
                5256000 => "永久禁言",
            ])->required();

            $form->select('reason_type', '禁言原因')->options(UserFreezeLog::REASON_TYPE)->required();
            $form->text('reason', '原因说明');

            $form->saving(function (Form $form) {
                $user = UserProfile::where('user_profile_id', $form->user_id)->first();
                if (!$user) {
                    return InfoUtil::adminErr('用户信息不存在！');
                }

                $form->user_id = $user->user_id;

                if ($form->isCreating()) {
                    $m = $form->model();

                    $m->start_ts = time();
                    $m->end_ts = time() + $form->forbid_mins * 60;
                    $m->op_admin_id = Admin::user()->id;

                    $m->save();

                    UserForbidden::cancelForbidden($form->user_id);
                }
            });

            $form->saved(function (Form $form) {
                $user = User::find($form->user_id);
                $user->update(['status' => User::STATUS_FORBID]);

                $minutes = intval($form->forbid_mins);
                if ($minutes >= 5256000) {
                    $msg = "经审核发现，你的账号在平台出现违规行为，现将此账号作出永久禁言处理，禁言期间暂时无法使用私信等功能。如有疑问，请联系在线客服";
                } else {
                    $days = $minutes / 1440;
                    $msg = "经审核发现，你的账号在平台出现违规行为，现将此账号作出禁言 {$days} 天处理，禁言期间暂时无法使用私信等功能。如有疑问，请联系在线客服";
                }

                Message::pushSystem($form->user_id, $msg, '封禁通知');
            });
        });
    }
}
